package com.jdz.gxq.info.admin.enterprise.service;

import java.util.List;
import java.util.Map;

import jp.terasoluna.fw.dao.QueryDAO;
import jp.terasoluna.fw.dao.UpdateDAO;

import com.jdz.gxq.info.admin.enterprise.bo.Enterprise;

public class EnterpriseServiceImpl implements EnterpriseService {
	
	// 企业经济数据录入画面用的企业选择
	private static final String SELECT_BIZ_TYPE_ECONOMIC = "1";
	
	private QueryDAO queryDAO = null;
	
	private UpdateDAO updateDAO = null;
	
	/**
	 * Enterprise检索分页用，查询记录条数
	 * @return
	 */
	public int getEnterprisesCount(String enterpriseName) {
		
		int totalCount = queryDAO.executeForObject("enterprise.getEnterprisesCount",
				enterpriseName, Integer.class);
		
		return totalCount;
	}
	
	/**
	 * Enterprise检索
	 * @return
	 */
	public List<Enterprise> getEnterprises(Map<String, Object> params) {
		
		return queryDAO.executeForObjectList("enterprise.getEnterprises", params);
	}
	
	/**
	 * 追加Enterprise
	 * @param Economic
	 */
	public void insertEnterprise(Enterprise enterprise) {
		
		updateDAO.execute("enterprise.insertEnterprise", enterprise);
	}
	
	/**
	 * 更新Enterprise
	 * @param Economic
	 */
	public void updateEnterprise(Enterprise enterprise) {
		
		updateDAO.execute("enterprise.updateEnterprise", enterprise);
	}
	
	/**
	 * 通过id得到Enterprise
	 * @param id
	 * @return
	 */
	public Enterprise getEnterpriseById(int enterpriseId) {

		Enterprise enterprise = queryDAO.executeForObject("enterprise.getEnterpriseById",
				enterpriseId, Enterprise.class);
		
		return enterprise;
	}
	
	/**
	 * 删除Enterprise
	 * @param Economic
	 */
	public void deleteEnterprise(int enterpriseId) {
		
		updateDAO.execute("enterprise.deleteEnterpriseById", enterpriseId);
	}
	
	/**
	 * 验证EnterpriseName是否重复
	 * flag: 1 追加时验证
	 * flag: 2 更新时验证
	 */
	public boolean checkEnterpriseName(Enterprise paramEnterprise, int flag) {
		
		Enterprise enterprise = queryDAO.executeForObject("enterprise.getEnterpriseByName",
				paramEnterprise.getEnterpriseName(), Enterprise.class);

		if (enterprise == null) {
			return true;
		}
		
		// 更新验证，查出来的数据是不是本身
		if (flag == 2) {
			
			if (enterprise.getEnterpriseId() == paramEnterprise.getEnterpriseId()) {
				return true;
			}
		}
		
		return false;
	}
	
	/**
	 * 企业选择页面，Enterprise检索分页用，查询记录条数
	 * @return
	 */
	public int getSelectEnterprisesCount(String type, String enterpriseName) {
		
		int totalCount = 0;
		
		if (SELECT_BIZ_TYPE_ECONOMIC.equals(type)) {
			
			totalCount = queryDAO.executeForObject("enterprise.getEconomicEnterprisesCount",
					enterpriseName, Integer.class);
		}
		
		
		
		return totalCount;
	}
	
	/**
	 * 企业选择页面，Enterprise检索
	 * @return
	 */
	public List<Enterprise> getSelectEnterprises(String type, Map<String, Object> params) {

		List<Enterprise> enterpriseList = null;

		if (SELECT_BIZ_TYPE_ECONOMIC.equals(type)) {

			enterpriseList = queryDAO.executeForObjectList("enterprise.getEconomicEnterprises", params);
		}

		return enterpriseList;

	}

	public void setQueryDAO(QueryDAO queryDAO) {
		this.queryDAO = queryDAO;
	}

	public void setUpdateDAO(UpdateDAO updateDAO) {
		this.updateDAO = updateDAO;
	}
	
	
}
